/*
 * Copyright (c) 2008 Benoit Bouchez, Nicolas Giard
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
package com.noocodecommit.wicket.stuff.jquery.plugin.ui.resizable;

import com.noocodecommit.wicket.stuff.jquery.plugin.JQPlugin;

/**
 * 
 * @author Benoit Bouchez
 */
public class JQResizableUtils
{

	/**
	 * Create a JQResizableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @param key
	 *            plugin unique id on which component is attached
	 * @return JQResizableBuilder
	 */
	public static JQResizableBuilder create(String id, String key)
	{
		return new JQResizableBuilder(new JQResizable(id, (key == null ? JQResizablePlugin.DEFAULT_KEY : key)));
	}

	/**
	 * Create a JQResizableBuilder instance on which you can fluently change settings
	 * 
	 * @param id
	 *            wicket component id
	 * @return JQResizableBuilder
	 */
	public static JQResizableBuilder create(String id)
	{
		return new JQResizableBuilder(new JQResizable(id, null));
	}

	/**
	 * @author Benoit Bouchez
	 */
	public static final class JQResizableBuilder
	{
		/** JQuery component */
		private JQResizable component;
		/** JQuery default options */
		private JQResizableOptions options;

		/**
		 * Constructor
		 * 
		 * @param component
		 *            widget
		 */
		public JQResizableBuilder(JQResizable component)
		{
			this.component = component;
			this.options = new JQResizableOptions();
		}

		/**
		 * Sets plugin init mode
		 * 
		 * @param initMode
		 *            plugin init mode
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder initMode(int initMode)
		{
			component.getPlugin().setInitMode(initMode);
			return this;
		}

		/**
		 * Sets plugin selector when initMode is PLUGIN_INIT_MODE_ONCE
		 * 
		 * @param initOnceSelector
		 *            JQuery selector
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder initOnceSelector(String initOnceSelector)
		{
			component.getPlugin().setInitOnceSelector(initOnceSelector);
			return this;
		}

		/**
		 * Sets the auto hide option value
		 * 
		 * @param autohide
		 *            resizable auto hide
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder autohide(boolean autohide)
		{
			options.setAutohide(autohide);
			return this;
		}

		/**
		 * Sets the handles option value
		 * 
		 * @param handles
		 *            resizable handles
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder handles(String handles)
		{
			options.setHandles(handles);
			return this;
		}

		/**
		 * Sets the containment option value
		 * 
		 * @param containment
		 *            resizable containment
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder containment(String containment)
		{
			options.setContainment(containment);
			return this;
		}

		/**
		 * Sets the max height option value
		 * 
		 * @param maxHeight
		 *            resizable max height
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder maxHeight(int maxHeight)
		{
			options.setMaxHeight(maxHeight);
			return this;
		}

		/**
		 * Sets the max width option value
		 * 
		 * @param maxWidth
		 *            resizable max width
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder maxWidth(int maxWidth)
		{
			options.setMaxWidth(maxWidth);
			return this;
		}

		/**
		 * Sets the min height option value
		 * 
		 * @param minHeight
		 *            resizable min height
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder minHeight(int minHeight)
		{
			options.setMinHeight(minHeight);
			return this;
		}

		/**
		 * Sets the min width option value
		 * 
		 * @param minWidth
		 *            resizable min width
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder minWidth(int minWidth)
		{
			options.setMinWidth(minWidth);
			return this;
		}

		/**
		 * Sets the proxy option value
		 * 
		 * @param proxy
		 *            resizable proxy
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder proxy(String proxy)
		{
			options.setProxy(proxy);
			return this;
		}

		/**
		 * Sets the destructive option value
		 * 
		 * @param destructive
		 *            resizable destructive
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder destructive(boolean destructive)
		{
			options.setDestructive(destructive);
			return this;
		}

		/**
		 * Sets the proportionnally resize option value
		 * 
		 * @param proportionallyResize
		 *            resizable proportionnally resize
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder proportionallyResize(String proportionallyResize)
		{
			options.setProportionallyResize(proportionallyResize);
			return this;
		}

		/**
		 * Sets the start function
		 * 
		 * @param startScript
		 *            resizable start function
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder startScript(String startScript)
		{
			options.setStartScript(startScript);
			return this;
		}

		/**
		 * Sets the resize function
		 * 
		 * @param resizeScript
		 *            resizable start function
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder resizeScript(String resizeScript)
		{
			options.setResizeScript(resizeScript);
			return this;
		}

		/**
		 * Sets the stop function
		 * 
		 * @param stopScript
		 *            resizable stop function
		 * @return JQResizableBuilder
		 */
		public JQResizableBuilder stopScript(String stopScript)
		{
			options.setStopScript(stopScript);
			return this;
		}

		/**
		 * @return JQResizable
		 */
		public JQResizable build()
		{
			if (component.getPlugin().getInitMode() == JQPlugin.PLUGIN_INIT_ONCE)
				component.getPlugin().setOptions(options);
			else
				component.setOptions(options);
			return component;
		}
	}
}